NB-Asset is a simple asset management tool for IPython Notebook extensions. Other web asset management solutions cater to the needs of websites: Few http requests, small filesize, serving files through S3 or NGINX and so on. These concerns don't matter as much in the notebook interface.
Features and Goals:
In [1]:
import nb_assets
In [2]:
assets = [("coffeescript", "test.coffee"),
("javascript", "hello_javascript.js")]
nb_assets.display_assets(assets, input_dir="assets", output_dir="static")
The tuples in the assets list refer to a recipe and a source files. Recipes compile and load the asset into the page. Currently only coffeescript and javascript are available.
"input_dir" refers to the source directory, and "output_dir" refers to the output directory, in this case as path names relative to the notebook file.
The coffeescript files are only compiled if they have changed since the last compilation. This allows for efficient development, but it also means that other developers don't need a coffeescript compiler as long as they don't change the coffeescript source code.
That's important, because in general, IPython Notebook users who use an extension in a python library or download a cool notebook file, aren't guaranteed to have a coffeescript compiler. NB-Assets makes it easy to hide the magic!
If you wrote a cool widget for a python library, you will probably want to include your assets into the library itself. The easiest way is to have an input directory inside the libraries base directory, and resolve it like so:
assets = [("coffeescript", "test.coffee"),
("javascript", "hello_javascript.js")]
def output_notebook():
import nb_assets
base_dir = os.path.dirname(__file__)
input_dir = os.path.join(os.path.dirname(nb_assets.__file__), 'assets', 'src')
output_dir = os.path.join(base_dir, 'assets', 'compiled')
nb_assets.display_assets(assets, input_dir=input_dir, output_dir=output_dir)
Some caveats:
There are various implementations of a coffeescript magic floating around the internet, but as far as I know, none in a distributable package. Because such a cell magic is really useful when developing notebook features, NB-assets contains such a cell magic. This implementation does not offer source maps, though.
In [3]:
import nb_assets
nb_assets.load_magics()
In [5]:
%%coffeescript -vb
console.log "Hello World from Coffeescript magic"
for i in [1..10]
console.log i
In [ ]: